package com.keyword.utils;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.function.Function;

/**
 * @author yhx
 */
public class Try {

    public static <T,R> Function<T,R> of (UncheckedFunction<T,R> mapper) {
        Objects.requireNonNull(mapper);
        return t -> {
            try {
                return mapper.apply(t);
            } catch (Exception e) {
                throw Exceptions.unchecked(e);
            }
        };
    }

    @FunctionalInterface
    public interface UncheckedFunction<T,R> {
        /**
         * 执行
         * @param t
         * @return
         * @throws Exception
         */
        R apply(T t) throws Exception;
    }

    public static void main(String[] args) {
        //简化lambda异常处理
        List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5, 6, 0);
        integers.forEach(i -> System.out.println(50 / i));
    }
}
